
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


import messagepasser.DocInfo;
import messagepasser.DocListDAOSQL;
import messagepasser.DocPermissions;
import messagepasser.LoginDAOSQL;
import messagepasser.PermissionsDAOSQL;
import messagepasser.UserInfo;
import messagepasser.UserInfoDAOSQL;

public class Server_Servant extends UnicastRemoteObject implements Server {
	
	Map <Integer, Client> onlineUsers = new HashMap <Integer, Client>();
		
	public Server_Servant() throws RemoteException {
	}	
	public UserInfo login (String user, String pwd, String ipAddr, int port) throws RemoteException {
		UserInfo userInformation = null;
		LoginDAOSQL logindao = new LoginDAOSQL("com.mysql.jdbc.Driver","jdbc:mysql:///tecbox","userprofile");
		String pwddb = logindao.getPwd(user);
		if(pwddb!=null){
			if (pwddb.equals(pwd)){ 
				System.out.println("Login Successful");
				int userid = logindao.getUserID(user);
				userInformation = new UserInfo(userid, user, true, ipAddr, port);
				logindao.setIpPort(ipAddr, port, userid);
			}
			else {
				System.out.println("Login Successful");
			}
		}
		else {
			System.out.println("Login Unsuccessful");
			return null;
		}
		
		Set <Integer> userIDs = onlineUsers.keySet();
		for (Integer i : userIDs) {
			try {
				onlineUsers.get(i).newUserOnline(userInformation);
			}
			catch (Exception e) {
				onlineUsers.remove(i);
				System.out.println ("user may be offline");
			}
		}
		
		return userInformation; 			

	}
	
	public List<DocInfo> getDocuments(int userId) throws RemoteException {
	
		DocListDAOSQL docDAO = new DocListDAOSQL("com.mysql.jdbc.Driver","jdbc:mysql:///tecbox","document");
		return docDAO.getDocsInfo(userId);
		 			
	}
	
	public UserInfo[] getonlineUsers(int userId) throws RemoteException {
	// TODO Auto-generated method stub
		UserInfoDAOSQL userDAO = new UserInfoDAOSQL("com.mysql.jdbc.Driver","jdbc:mysql:///tecbox","user");
		return userDAO.getuserinfo(userId);
	}
	@Override
	public void register(Client c, int userId) throws RemoteException {
		this.onlineUsers.put(userId, c);
	}
	@Override
	public DocPermissions[] getDocUsers(int docId) throws RemoteException {
		PermissionsDAOSQL perDAO = new PermissionsDAOSQL("com.mysql.jdbc.Driver","jdbc:mysql:///tecbox","user");
		return perDAO.getDocCollaborators(docId);
		
	}
	@Override
	public void logout(UserInfo user) throws RemoteException {
		
		LoginDAOSQL logindao = new LoginDAOSQL("com.mysql.jdbc.Driver","jdbc:mysql:///tecbox","userprofile");
		logindao.removeIpPort(user.getUserid());
		onlineUsers.remove(user.getUserid());
		System.out.println ("user offline request");
		Set <Integer> userIDs = onlineUsers.keySet();
		for (Integer id : userIDs) {
			try {
				onlineUsers.get(id).userOffline(user);
			}
			catch (Exception e) {
			//	onlineUsers.remove(i);
				System.out.println ("user may be offline");
			}
		}
		
	}
	@Override
	
	//added for client req or chat
	public void regForChat(UserInfo src,UserInfo dest) throws RemoteException {
		
		onlineUsers.get(src.getUserid()).acceptRequest(dest);
		onlineUsers.get(dest.getUserid()).acceptRequest(src);
		
		
	}
	
	
}
